package com.zhiche.lisa.bms.controller.driver;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.pojo.vo.driver.VehicleClassifyVO;
import com.zhiche.lisa.bms.service.driver.IVehicleClassifyService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 车型分类表 前端控制器
 * </p>
 *
 * @author zhaoguixin
 * @since 2018-08-30
 */
@RestController
@RequestMapping("/vehicle_classify")
@Slf4j
@Api(description = "车型分类", tags = {"车型分类"})
public class VehicleClassifyController {

    @Autowired
    private IVehicleClassifyService vehicleClassifyService;

    /**
     * 分页显示车型
     *
     * @param page
     * @return
     */
    @PostMapping(value = "/page_list")
    @ApiOperation(value = "分页显示车型", notes = "分页显示车型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = VehicleClassifyVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<VehicleClassifyVO>> listByPage(@ApiParam(name = "VehicleClassifyVO", value = "车型VO", required = true) @RequestBody Page<VehicleClassifyVO> page) {
        log.info("VehicleClassifyController.listByPage params:{}", page);
        RestfulResponse<Page<VehicleClassifyVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<VehicleClassifyVO> pageVo = vehicleClassifyService.listByPage(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            log.error("VehicleClassifyController.listByPage ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("VehicleClassifyController.listByPage ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 添加车型
     *
     * @param vehicleClassifyVO
     * @param br
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "添加车型", notes = "添加车型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Integer.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Integer> add(@ApiParam(name = "VehicleClassifyVO", value = "车型VO", required = true) @RequestBody @Valid VehicleClassifyVO vehicleClassifyVO, BindingResult br) {
        log.info("VehicleClassifyController.add params:{}", vehicleClassifyVO);
        RestfulResponse<Integer> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Integer id = vehicleClassifyService.add(vehicleClassifyVO, br);
            result.setData(id);
        } catch (BaseException e) {
            log.error("VehicleClassifyController.add ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("VehicleClassifyController.add ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 更新车型
     *
     * @param vehicleClassifyVO
     * @param br
     * @return
     */
    @PostMapping(value = "/update")
    @ApiOperation(value = "更新车型", notes = "更新车型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Boolean> update(@ApiParam(name = "VehicleClassifyVO", value = "车型VO", required = true) @RequestBody @Valid VehicleClassifyVO vehicleClassifyVO, BindingResult br) {
        log.info("VehicleClassifyController.update params:{}", vehicleClassifyVO);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blnRtn = vehicleClassifyService.update(vehicleClassifyVO, br);
            result.setData(blnRtn);
        } catch (BaseException e) {
            log.error("VehicleClassifyController.update ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("VehicleClassifyController.update ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/enableOrDisabled")
    @ApiOperation(value = "禁用/启用", notes = "禁用/启用",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "Integer", name = "id", value = "车型ID", required = true), @ApiImplicitParam(paramType = "form", dataType = "Boolean", name = "enableOrDisabled", value = "启用和禁用状态", required = true)})
    public RestfulResponse<Boolean> enableOrDisabled(Integer id, boolean enableOrDisabled) {
        log.info("VehicleClassifyController.enableOrDisabled params: {}, {}", id, enableOrDisabled);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Boolean blrRtn = vehicleClassifyService.enableOrDisabled(id, enableOrDisabled);
            result.setData(blrRtn);
        } catch (BaseException e) {
            log.error("VehicleClassifyController.enableOrDisabled ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.listByPage enableOrDisabled:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 根据ID得到车型信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/getById")
    @ApiOperation(value = "根据ID得到车型信息", notes = "根据ID得到车型信息",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = VehicleClassifyVO.class), @ApiResponse(code = -1, message = "系统异常")})
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "车型ID", required = true)})
    public RestfulResponse<VehicleClassifyVO> getById(Integer id) {
        log.info("VehicleClassifyController.getById params: {}", id);
        RestfulResponse<VehicleClassifyVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            VehicleClassifyVO vehicleClassifyVO = vehicleClassifyService.getById(id);
            result.setData(vehicleClassifyVO);
        } catch (BaseException e) {
            log.error("RouteController.getById ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.getById ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    /**
     * 删除车型
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/del")
    @ApiOperation(value = "删除车型", notes = "删除车型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Boolean.class), @ApiResponse(code = -1, message = "系统异常")})
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form", dataType = "Integer", name = "id", value = "车型ID", required = true)})
    public RestfulResponse<Boolean> del(Integer id) {
        log.info("VehicleClassifyController.del params: {}", id);
        RestfulResponse<Boolean> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(vehicleClassifyService.del(id));
        } catch (BaseException e) {
            log.error("RouteController.del ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.del ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "列表显示车辆类型", notes = "列表显示车辆类型",
            produces = MediaType
                    .APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "key", value = "车辆名称")})
    public RestfulResponse<List<VehicleClassifyVO>> list(String key) {
        log.info("VehicleClassifyController.list params: {}", key);
        RestfulResponse<List<VehicleClassifyVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(vehicleClassifyService.listByName(key));
        } catch (BaseException e) {
            log.error("RouteController.list ERROR:{}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            log.error("RouteController.list ERROR:{} ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

}

